const User = require('../modules/users')
const jsonwebtoken = require('jsonwebtoken')
// const jwt = require('koa-jwt')
const {
    secret
} = require('../config')

class usersCtl {
    async find(ctx) {
        ctx.body = await User.find()
    }

    async findById(ctx) {
        const {
            fields = ''
        } = ctx.query
        const fieldsStr = fields.split(';').filter(f => f).map(f => ' +' + f).join('')
        const user = await User.findById(ctx.params.id).select(fieldsStr)
        if (!user) {
            ctx.throw(404, '用户不存在')
        }
        ctx.body = user
    }

    async create(ctx) {
        ctx.verifyParams({
            name: {
                type: 'string',
                required: true
            },
            passward: {
                type: 'string',
                required: true
            }
        })
        const user = await new User(ctx.request.body).save()
        ctx.body = user
    }

    async update(ctx) {
        ctx.verifyParams({
            name: {
                type: 'string',
                required: false
            },
            passward: {
                type: 'string',
                required: false
            },
            avatar_url: {
                type: 'string',
                required: false
            },
            gender: {
                type: 'string',
                required: false
            },
            headline: {
                type: 'string',
                required: false
            },
            locations: {
                type: 'array',
                itemType: 'string',
                required: false
            },
            employments: {
                type: 'array',
                itemType: 'object',
                required: false
            },
            educations: {
                type: 'array',
                itemType: 'object',
                required: false
            }
        })
        const user = await User.findByIdAndUpdate(ctx.params.id, ctx.request.body)
        if (!user) {
            ctx.throw(404, '用户不存在')
        }
        ctx.body = user
    }

    async delete(ctx) {
        const user = await User.findByIdAndRemove(ctx.params.id, ctx.request.body)
        if (!user) {
            ctx.throw(404, '用户不存在')
        }
        ctx.status = 204
    }

    async login(ctx) {
        ctx.verifyParams({
            name: {
                type: 'string'
            },
            passward: {
                type: 'string'
            }
        })
        const user = await User.findOne(ctx.request.body)
        if (!user) {
            ctx.throw(401, '账号或密码错误')
        }
        const {
            _id,
            name
        } = user
        const token = jsonwebtoken.sign({
            _id,
            name
        }, secret, {
            expiresIn: '1d'
        })
        ctx.body = {
            token
        }
    }

    async ListFollowing(ctx) {
        const user = await await User.findById(ctx.params.id).select('+following').populate('following')
        if (!user) {
            ctx.throw(404)
        }
        ctx.body = user.following
    }

    async ListFollower(ctx) {
        const users = await User.find({following: ctx.params.id})
        ctx.body = users
    }

    async checkUserExist(ctx,next){
        const user = await User.findById(ctx.params.id)
        if(!user){
            ctx.throw(404,'用户不存在')
        }
        await next()
    }

    async follow(ctx) {
        const me = await User.findById(ctx.state.user._id).select('+following')
        if (!me.following.map(id => id.toString()).includes(ctx.params.id)) {
            me.following.push(ctx.params.id)
            me.save()
        }
        ctx.status = 204
    }

    async unfollow(ctx) {
        const me = await User.findById(ctx.state.user._id).select('+following')
        const index = me.following.map(id => id.toString()).indexOf(ctx.params.id)
        if (index > -1) {
            me.following.splice(index, 1)
            me.save()
        }
        ctx.status = 204
    }
}

module.exports = new usersCtl()